package com.have.blog.web.service.impl;

import com.have.blog.base.enums.BlogPublishStatus;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.model.mapper.SysDictDataMapper;
import com.have.blog.model.mapper.SysDictTypeMapper;
import com.have.blog.model.models.SysDictData;
import com.have.blog.model.models.SysDictDataExample;
import com.have.blog.model.models.SysDictType;
import com.have.blog.model.models.SysDictTypeExample;
import com.have.blog.web.service.SysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@SuppressWarnings("all")
public class SysDictDataServiceImpl implements SysDictDataService {
    @Autowired
    private SysDictDataMapper dictDataMapper;
    @Autowired
    private SysDictTypeMapper dictTypeMapper;

    @Override
    public Map<String, Map<String, Object>> getListByDictTypeList(List<String> dictTypeList) {
        Map<String, Map<String, Object>> map = new HashMap<>();
        List<String> tempTypeList = new ArrayList<>();

        // 查询 dict_type 在 tempTypeList中的
        SysDictTypeExample typeExample = new SysDictTypeExample();
        typeExample.createCriteria()
                .andDictTypeIn(dictTypeList)
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andIsPublishEqualTo(BlogPublishStatus.PUBLISH.getStatus());
        List<SysDictType> typeList = dictTypeMapper.selectByExample(typeExample);
        // 根据dict_type字段查询 dictType

        for (SysDictType dictType : typeList) {
            SysDictDataExample dataExample = new SysDictDataExample();
            // 根据字典类型查询字典数据
            dataExample.createCriteria()
                    .andIsPublishEqualTo(BlogPublishStatus.PUBLISH.getStatus())
                    .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                    .andDictTypeUidEqualTo(dictType.getUid());
            dataExample.setOrderByClause("sort desc");
            List<SysDictData> dictDataList = dictDataMapper.selectByExample(dataExample);
            String defaultValue = null;
            if (!CollectionUtils.isEmpty(dictDataList)) {
                defaultValue = dictDataList.get(0).getDictValue();
            }
            Map<String, Object> result = new HashMap<>();
            result.put("defaultValue", defaultValue);
            result.put("list", dictDataList);
            map.put(dictType.getDictType(), result);
        }
        return map;
    }
}
